package com.lazar.bigdata.flume;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.val;
import org.apache.commons.compress.utils.Charsets;
import org.apache.flume.Context;
import org.apache.flume.Event;
import org.apache.flume.event.SimpleEvent;
import org.apache.flume.interceptor.Interceptor;
import org.junit.Test;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CustomerInterceptor implements Interceptor {
    @Override
    public void initialize() {

    }

    @Override
    public Event intercept(Event event) {
        // 获取event header
        Map<String,String> eventHeader = event.getHeaders();
        // 获取body
        String eventBody = new String(event.getBody(), Charsets.UTF_8);
        // 获取json 串
        String[] bodyArr = eventBody.split("\\s+");
        try{
            String jsonStr = bodyArr[6];
            // 解析json
            JSONObject obj = JSON.parseObject(jsonStr);
            String timestamp =  obj.getJSONObject("app_active").getString("time");

            // 时间戳字符串转 YYYY-dd-MM
            long tm = Long.parseLong(timestamp);
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            Instant instant = Instant.ofEpochMilli(tm);
            LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
            String date = formatter.format(localDateTime);

            //将日期字符放入event header
            eventHeader.put("logtime",date);
            event.setHeaders(eventHeader);
        }catch (Exception e){
            eventHeader.put("logtime","unknown");
            event.setHeaders(eventHeader);
        }
        return event;
    }

    @Override
    public List<Event> intercept(List<Event> list) {
        List<Event> lstEvent = new ArrayList<>();
        for (Event event: list){
            Event outEvent = intercept(event);
            if (outEvent != null){
                lstEvent.add(outEvent);
            }
        }
        return lstEvent;
    }

    @Override
    public void close() {

    }

    public static class Builder implements Interceptor.Builder{

        @Override
        public Interceptor build() {
            return new CustomerInterceptor();
        }

        @Override
        public void configure(Context context) {

        }
    }

    @Test
    public void testFlumeInterceptor(){
        String log = "2020-08-20 11:56:08.211 [main] INFO  com.lagou.ecommerce.AppStart - {\"app_active\":{\"name\":\"app_active\",\"json\":{\"entry\":\"2\",\"action\":\"0\",\"error_code\":\"0\"},\"time\":1595288248066},\"attr\":{\"area\":\"三门峡\",\"uid\":\"2F10092A1\",\"app_v\":\"1.1.0\",\"event_type\":\"common\",\"device_id\":\"1FB872-9A1001\",\"os_type\":\"0.97\",\"channel\":\"WM\",\"language\":\"chinese\",\"brand\":\"xiaomi-3\"}}";
        Map<String,String> map = new HashMap<>();

        Event simpleEvent = new SimpleEvent();
        simpleEvent.setHeaders(map);
        simpleEvent.setBody(log.getBytes(Charsets.UTF_8));

        CustomerInterceptor customerInterceptor = new CustomerInterceptor();
        Event outEvent = customerInterceptor.intercept(simpleEvent);

        System.out.println(JSON.toJSONString(outEvent.getHeaders()));
    }
}
